import { _decorator, Button, Label, tween, Tween, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import GameDC from '../../data/data.game.center';
import ConfManager from '../../manager/config.manager';
import DataManager from '../../manager/data.manager';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppActiveMaster')
export class ppActiveMaster extends BasePopUp<typeof Popup, boolean> {
	protected default_return: null;

	private ui_ligth: UITransform;
	private contianer: UITransform;

	protected initAfterOnLoad(){
		this.init();
		this.initUIStatus();
		this.renderLocalUI();
	}

	public onShow(){
		BasePopUp.jelly_enter(this.contianer.node);

		Tween.stopAllByTarget(this.ui_ligth.node);
		tween(this.ui_ligth.node).set({angle: 0})
		.to(5, {angle: -360}).union()
		.repeatForever().start();
	}

	private init(){
		this.btn_lock = false;
		let node_box = this.node.getChildByName('box');
		this.ui_ligth = node_box.getChildByName('light').getComponent(UITransform);
		this.contianer = node_box.getChildByName('container').getComponent(UITransform);
	}

	private initUIStatus(){
		this.ui_ligth.node.angle = 0;
	}

	private renderLocalUI(){

		this.contianer.node.getChildByName('lbl-head').getComponent(Label).string = 'MASTER PASS!';
		DataManager.GoodsSynced
		.then(()=>DataManager.getGoodsInfo(ConfManager.otherConf.buffPrice.masterPassGoodsId))
		.then(info=>{
			this.contianer.node.getChildByName('lbl-master').getComponent(Label).string = info.localPrice;
		});
	}

	private btn_lock: boolean;
	protected _click_event_: { [name: string]: (this: ppActiveMaster, button: Button) => void; } = {
		close(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			this.exit(false);
		},
		'active-master'(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			this.pop.Wait(DataManager.activeMasterPass())
			.then(()=>{
				let success = GameDC.User.master;
				if(success){
					this.exit(success);
				}else{
					this.btn_lock = false;
				}
			});
		}
	}

	private exit(active: boolean){
		BasePopUp.jelly_leave(this.contianer.node)
		.then(()=>{
			this.close(active);
			this.btn_lock = false;
			Tween.stopAllByTarget(this.ui_ligth.node);
			this.ui_ligth.node.angle = 0;
		});
	}
}
